{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Streaming\n",
    "All LLMs implement the Runnable interface, which comes with default implementations of all methods, ie. ainvoke, batch, abatch, stream, astream. This gives all LLMs basic support for streaming.<br>\n",
    "所有 LLM s 实现接口 Runnable ，该接口附带所有方法的默认实现，即。ainvoke、batch、abatch、stream、astream。这为流式处理提供了所有 LLM 基本支持。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Streaming support defaults to returning an Iterator (or AsyncIterator in the case of async streaming) of a single value, the final result returned by the underlying LLM provider. This obviously doesn't give you token-by-token streaming, which requires native support from the LLM provider, but ensures your code that expects an iterator of tokens can work for any of our LLM integrations.<br>\n",
    "流式处理支持默认返回单个值的迭代器（或异步流式处理中的 AsyncIterator），即基础 LLM 提供程序返回的最终结果。这显然不会为您提供逐个令牌的流式处理，这需要 LLM 提供商的本机支持，但可以确保需要令牌迭代器的代码可以适用于我们的 LLM 任何集成。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Verse 1:\n",
      "Sipping on the bubbles, it's a refreshing sound\n",
      "Gives me that kick, when I'm feeling down\n",
      "Sparkling water, with no added sugar\n",
      "A healthier choice, my taste buds eagerly discover\n",
      "\n",
      "Chorus:\n",
      "Oh, sparkling water, how you make me feel alive\n",
      "Bursts of flavor, as I take another dive\n",
      "No more soda, this will be my new addiction\n",
      "Sparkling water, a fizzy, guilt-free sensation\n",
      "\n",
      "Verse 2:\n",
      "From lemon to lime, or even raspberry\n",
      "So many options, it's hard to choose just one, you see\n",
      "But no matter the flavor, it always quenches my thirst\n",
      "No more cravings, for unhealthy drinks, that's the worst\n",
      "\n",
      "Chorus:\n",
      "Oh, sparkling water, how you make me feel alive\n",
      "Bursts of flavor, as I take another dive\n",
      "No more soda, this will be my new addiction\n",
      "Sparkling water, a fizzy, guilt-free sensation\n",
      "\n",
      "Bridge:\n",
      "Some say it's just plain water, with added gas\n",
      "But to me, it's like a party in a glass\n",
      "It's got that sparkle, that just can't be denied\n",
      "A sparkling water lover, I'll be till the day I die\n",
      "\n",
      "Chorus:\n",
      "Oh, sparkling water, how you make me feel alive\n",
      "Bursts of flavor, as I take another dive\n",
      "No more soda, this will be my new addiction\n",
      "Sparkling water, a fizzy, guilt-free sensation\n",
      "\n",
      "Outro:\n",
      "So raise up your glass, and let's make a toast\n",
      "To sparkling water, the drink we love the most\n",
      "A healthier choice, for our body and our soul\n",
      "Sparkling water, you'll always have my heart and my control."
     ]
    }
   ],
   "source": [
    "from langchain_openai import OpenAI\n",
    "\n",
    "llm = OpenAI(model=\"gpt-3.5-turbo-instruct\", temperature=0, max_tokens=512)\n",
    "for chunk in llm.stream(\"Write me a song about sparkling water.\"):\n",
    "    print(chunk, end=\"\", flush=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain0_1",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
